﻿using System;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
	/**
	 * Classname : Role
	 * Version information :1.0
	 * Date  :26-09-2006
	 * Copyright notice : 
	 */
	
	public class DARole : DbObject
	{
		 protected SqlConnection connec;

         public DARole(SqlConnection Connection)
		{
            connec = Connection;
		}	


		// return all the Roles select Language
		// Danh cho nguoi quan tri
		public DataSet loadRoleStatus(bool RoleStatus)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@RoleStatus", SqlDbType.Bit) };
			parameters[0].Value = RoleStatus;
            return RunProcedure("sp_Roles_loadRoleStatus", "Tb_Roles", connec);
		}

		public DataSet loadRole()
		{
			// create the parameter
            DataSet dsRole = RunProcedure("sp_Roles_loadRole", "Tb_Roles", connec);
            for (int i = 0; i < dsRole.Tables[0].Rows.Count; i++)
            {
                dsRole.Tables[0].Rows[i]["Seq"] = (i + 1).ToString();
            }
            return dsRole;
		}
		
		// return only the record with the specified ID!!!!!!!!!!!
		public DataRow loadRoleID(int RoleID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
			parameters[0].Value = RoleID;

            using (DataSet categorys = RunProcedure("sp_Roles_loadRoleID", parameters, "Tb_Roles", connec))
			{
				return categorys.Tables[0].Rows[0];
			}
		}	
		// check or uncheck the Archived status of the specified question
		// Cap nhat trang thai (kich hoat) cho tat ca ban ghi duoc chon
		public int updateRoleStatus(string RoleIDs)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@RoleIDs", SqlDbType.VarChar,200)	};
			parameters[0].Value = RoleIDs;			
			// run the procedure
            RunProcedure("sp_Roles_updateRoleStatus", parameters, out numAffected, connec);

			return numAffected;			
		}

		// update the properties of the specified Roles
		// Sua noi dung ban ghi hien hanh
		public int updateRole(int RoleID, string RoleName, bool RoleStatus)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = { 	new SqlParameter("@RoleID", SqlDbType.Int, 4),
											new SqlParameter("@RoleName", SqlDbType.NVarChar, 64),											
											new SqlParameter("@RoleStatus", SqlDbType.Bit, 1)
										};
			// set the values
			parameters[0].Value = RoleID;
			parameters[1].Value = RoleName.Trim();
			parameters[2].Value = RoleStatus;			
			// run the procedure
            RunProcedure("sp_Roles_updateRole", parameters, out numAffected, connec);

			return numAffected;
		}

		// add a new Role
		// Them ban ghi hien hanh
		public int insertRole(string RoleName,bool RoleStatus)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters =  {	 new SqlParameter("@RoleName", SqlDbType.NVarChar, 64),
											 new SqlParameter("@RoleStatus", SqlDbType.Bit),
											 new SqlParameter("@RoleID", SqlDbType.Int ,4)
										 };	
			// set the values
			parameters[0].Value = RoleName.Trim();
			parameters[1].Value = RoleStatus;
			parameters[2].Direction = ParameterDirection.Output;
		
			// run the procedure
            RunProcedure("sp_Roles_insertRole", parameters, out numAffected, connec);

			return (int)parameters[2].Value;
		}
		// Xoa RoleModule
		public int removeRoleModule(int RoleID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@RoleID", SqlDbType.Int)	
										};
			parameters[0].Value = RoleID;			
			// run the procedure
            RunProcedure("sp_RoleModule_removeRoleModule", parameters, out numAffected, connec);
			return numAffected;			
		}
		// Xoa RoleModule
		public int removeRoleUser(int RoleID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@RoleID", SqlDbType.Int)	
										};
			parameters[0].Value = RoleID;			
			// run the procedure
            RunProcedure("sp_RoleModule_removeRoleUser", parameters, out numAffected, connec);
			return numAffected;			
		}
		// Xoa RoleModule
		public int removeRoleModuleLanguage(int RoleID,int LanguageID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@RoleID", SqlDbType.Int),	
											new SqlParameter("@LanguageID", SqlDbType.Int)	
										};
			parameters[0].Value = RoleID;
			parameters[1].Value = LanguageID;
			// run the procedure
            RunProcedure("sp_RoleModule_removeRoleModuleLanguage", parameters, out numAffected, connec);
			return numAffected;			
		}
		// Xoa Role
		public int removeRole(int RoleID)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters = {	new SqlParameter("@RoleID", SqlDbType.Int)	};
			parameters[0].Value = RoleID;			
			// run the procedure
            RunProcedure("sp_Roles_removeRoles", parameters, out numAffected, connec);
			return numAffected;			
		}
		// Load Module Role
		public DataSet loadRoleModuleforRoleID(int RoleID)
		{
			// create the parameter
			SqlParameter[] parameters = { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
			parameters[0].Value = RoleID;

            return RunProcedure("sp_RoleModule_loadRoleModuleforRoleID", parameters, "Tb_RoleModule", connec);			
		}
		// add a new RoleModule
		// Them ban ghi hien hanh
		public int insertRoleModule(int RoleID, int ModuleID, bool Read, bool Write, bool Update, bool Delete)
		{
			int numAffected;
			// create the parameters
			SqlParameter[] parameters =  {	 new SqlParameter("@RoleID", SqlDbType.Int),
											 new SqlParameter("@ModuleID", SqlDbType.Int),
											 new SqlParameter("@Read", SqlDbType.Bit),
											 new SqlParameter("@Write", SqlDbType.Bit),
											 new SqlParameter("@Update", SqlDbType.Bit),
											 new SqlParameter("@Delete", SqlDbType.Bit)
										 };	
			// set the values
			parameters[0].Value = RoleID;
			parameters[1].Value = ModuleID;
			parameters[2].Value = Read;
			parameters[3].Value = Write;
			parameters[4].Value = Update;
			parameters[5].Value = Delete;			
			// run the procedure
            RunProcedure("sp_RoleModule_insertRoleModule", parameters, out numAffected, connec);

			return numAffected;
		}		
	}
}